//
//  XSBrandViewController.m
//  MeiLiYiChu
//
//  Created by mac on 16/2/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSBrandViewController.h"
#import "SearchViewController.h"
#import "XSButton.h"
#import "XSMianDetailViewController.h"

#import "XSBrand.h"
@interface XSBrandViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UISearchBarDelegate>
{
    UISearchBar * _search;
    UITableView * _table;
    NSArray * array;
    UILabel * _lab;
    NSInteger _current;
    UIScrollView * _scroll;
    UICollectionView * _collection;
    UICollectionViewFlowLayout * layout;
    //当前点击按钮对应的图片个数
    NSInteger imageCount;
    
    NSInteger j;
    
    NSArray * array3;
}
@end

@implementation XSBrandViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden =YES;
    [XSHTTPManager getBrandPlistWithComplection:^(NSMutableArray *array1, NSMutableArray *array2) {
        _imageArray =array1;
        
        _imgArr =array2;
        NSLog(@"%ld++",(unsigned long)_imageArray.count);
        [_collection reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    array =[NSArray arrayWithObjects:@"上衣",@"裙子",@"裤子",@"饰品",@"女包",@"女鞋",@"美妆", nil];
    _search =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 40)];
    _search.delegate =self;
    _search.showsCancelButton =YES;
    
    _search.placeholder =@"请输入搜索名称";
    
    [self.view addSubview:_search];
    
    
    _lab =[[UILabel alloc]initWithFrame:CGRectMake(2, 60, 3,50)];
    _lab.backgroundColor =[UIColor magentaColor];
    [self.view addSubview:_lab];
    
    [self createScroll];
    [self createCollection];
}
-(void)createCollection
{
    layout =[[UICollectionViewFlowLayout alloc]init];
    //等宽
    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width -100-40)/3, ([UIScreen mainScreen].bounds.size.width -100-40)/3+20);
    
    layout.sectionInset =UIEdgeInsetsMake(15, 10, 10, 5);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing =5;
    
    
    _collection =[[UICollectionView alloc]initWithFrame:CGRectMake(100, 60, self.view.bounds.size.width-100, self.view.bounds.size.height-60) collectionViewLayout:layout];
    _collection.delegate =self;
    _collection.dataSource =self;
    [self.view addSubview:_collection];
    
    _collection.backgroundColor =[UIColor whiteColor];
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
   
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    
    //根据当前点击的第几个按钮取出
    
//    NSDictionary * dic =_imgArr[_current-1];
//    
//    array3 =dic[@"sub_categories"];
//    
    
    NSArray * arr =_imageArray[_current -1];
    NSLog(@"%ld ---arr",arr.count);

    return arr.count;


    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell =[_collection dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  
    //解决collection 重用问题
    UIImageView * imageView =(UIImageView *)[cell.contentView viewWithTag:cell.tag+100];
    if (imageView ==nil)
    {
        imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, layout.itemSize.width, layout.itemSize.width)];
        imageView.tag =cell.tag+100;
        
        
        [cell.contentView addSubview:imageView];
    }
    
   
    
    UILabel * lab =(UILabel *)[cell.contentView viewWithTag:cell.tag +10];
    if (lab ==nil)
    {
        lab =[[UILabel alloc]init];
        
        lab.center =CGPointMake(imageView.center.x, imageView.center.y+45);
        
        lab.bounds =CGRectMake(0, 0, layout.itemSize.width, 20);
        lab.font =[UIFont systemFontOfSize:13];
        lab.textAlignment =NSTextAlignmentCenter;
        
        lab.tag =cell.tag +10;
        
        [cell.contentView addSubview:lab];
    }
    cell.layer.borderColor =[UIColor blackColor].CGColor;
    cell.layer.borderWidth = 2;
    
    NSArray * arr =_imageArray[_current-1];
    XSBrand * brand =arr[indexPath.row];
    
     [imageView sd_setImageWithURL:[NSURL URLWithString:brand.Brandpic_url]];
    
      lab.text = brand.BrandTitle;

  
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell =[_collection cellForItemAtIndexPath:indexPath];
    
    UILabel * lab =(UILabel *)[cell.contentView viewWithTag:cell.tag+10];
    XSMianDetailViewController * searVC =[[XSMianDetailViewController alloc]init];
    searVC.title =lab.text;
    
    [self.navigationController pushViewController:searVC animated:YES];
}

#pragma mark - 左边视图
-(void)createScroll
{
    _scroll =[[UIScrollView alloc]initWithFrame:CGRectMake(5, 60, 80, self.view.bounds.size.height)];
    _scroll.contentSize = CGSizeMake(80, _scroll.frame.size.height*1.3);
    _scroll.backgroundColor =[[UIColor alloc]initWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    
    for (int i = 0; i< array.count; i++)
    {
        XSButton *button = [XSButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, i *50, _scroll.frame.size.width, 50);
        button.tag =i+1;
        [button setTitle:array[i] forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor magentaColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(btnCilck:) forControlEvents:UIControlEventTouchUpInside];
        [_scroll addSubview:button];
        
        if (i==0)
        {
            //第一个按钮的状态
            UIButton *btn =(UIButton *)[_scroll viewWithTag:1];
            [btn setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];
            _current =btn.tag;
            /////////自定义的状态不能用,上面的btn.tag值也不能等于i!
            //////
            /////
            //            btn.isSelect =YES;
            
            btn.selected =YES;
        }
    }
    [self.view addSubview:_scroll];
}
-(void)btnCilck:(UIButton *)button
{
    _lab.frame =CGRectMake(2, button.frame.origin.y+60, 3, button.frame.size.height);
    //之前的button状态
    UIButton * previouBtn =(UIButton *)[_scroll viewWithTag:_current];
    previouBtn.selected =NO;

    [previouBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
   
    //现在的button
    button.selected =YES;
    _current =button.tag;
    
    
    for (UICollectionView * collection in _collection.subviews)
    {
        //记录点击按钮状态的视图上
//        imageCount = _collection.subviews.count +imageCount;
        
        [collection removeFromSuperview];
    }
    
    [_collection reloadData];
    
}
//改变search上按钮的文字


-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    SearchViewController * searchVC =[[SearchViewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
